package com.sztest.jayjun.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;

import com.sztest.jayjun.BuildConfig;
import com.sztest.jayjun.MyApplication;
import com.sztest.jayjun.constant.Constant;
import com.sztest.jayjun.mmkv.MMKVUtils;

/*
 *  @项目名：  jayjun
 *  @包名：    com.sztest.jayjun.utils
 *  @文件名:   ToneOnClickListener
 *  @创建者:   okmin
 *  @创建时间:  2022/6/29 10:04
 *  @描述：    TODO
 */
public class ToneUtils {
    static boolean isPhone= BuildConfig.DEBUG;
    private static ToneGenerator mToneGenerator;
    public static void toneMusic() {
        if (!isPhone) {
            jniBeep();
            return;
        }
        if (mToneGenerator!=null) {
            mToneGenerator.stopTone();
            mToneGenerator.release();
            mToneGenerator=null;
        }
        AudioManager mAudioManager = (AudioManager)MyApplication.getInstance().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        int volume=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mToneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, volume);
        mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP, 300);
        LogUtils.e("play");
//        mToneGenerator.startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE, 200);
//        mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP, 200);
    }

    public static void toneMusic600() {
        if (!isPhone) {
            jniBeep();
            return;
        }
        if (mToneGenerator!=null) {
            mToneGenerator.stopTone();
            mToneGenerator.release();
            mToneGenerator=null;
        }
        AudioManager mAudioManager = (AudioManager)MyApplication.getInstance().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        int volume=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mToneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, volume);
        mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP, 600);
//        mToneGenerator.startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE, 200);
//        mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP, 200);
    }

    public static void jniBeep(){
        boolean select = MMKVUtils.getInstance().getBoolean(Constant.BUZZER);
        if (select){
            return;
        }
        MyApplication.getInstance().mHandler.removeCallbacks(mBeepclose);
        MyApplication.getInstance().mHandler.postDelayed(mBeepclose,110);
        MyApplication.getInstance().OnOffBuzzer(1);
    }

    static  Runnable mBeepclose =new Runnable() {
        @Override
        public void run() {
            LogUtils.e(mBeepclose+"=close=");
            MyApplication.getInstance().OnOffBuzzer(0);
        }
    };

    public static void jniBeepDiDi(){
        boolean select = MMKVUtils.getInstance().getBoolean(Constant.BUZZER);
        if (select){
            return;
        }
        MyApplication.getInstance().OnOffBuzzer(1);
        MyApplication.getInstance().mHandler.removeCallbacks(mBeepclose);
        MyApplication.getInstance().mHandler.postDelayed(mBeepclose,100);
        MyApplication.getInstance().mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                jniBeep();
            }
        }, 200);
    }

}
